﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClassForWinForm
{    
    public partial class Form8 : Form
    {
        public DataTable dt_Parent = new DataTable();
        public DataTable dt_Children = new DataTable();
        public Form8()
        {
            InitializeComponent();
        }

        private void Form8_Load(object sender, EventArgs e)
        {
            #region
            DataTable dt = new DataTable();
            dt.Columns.Add("Value");
            dt.Columns.Add("Text");

            dt.Rows.Add("1", "项目1");
            dt.Rows.Add("2", "项目2");
            dt.Rows.Add("3", "项目3");
            dt.Rows.Add("4", "项目4");
            dt.Rows.Add("5", "项目5");
            dt.Rows.Add("6", "项目6");

            comboBox1.DataSource = dt;
            comboBox1.DisplayMember = "Text";
            comboBox1.ValueMember = "Value";
            comboBox1.SelectedValue = "5";
            #endregion

            #region 多级联动数据源

            dt_Parent.Columns.Add("ParentID");
            dt_Parent.Columns.Add("ParentName");
            dt_Parent.Rows.Add("1", "北京市");
            dt_Parent.Rows.Add("2", "上海市");
            dt_Parent.Rows.Add("3", "河南省");
            dt_Parent.Rows.Add("4", "湖南省");
            dt_Parent.Rows.Add("5", "广东省");

            dt_Children.Columns.Add("ParentID");
            dt_Children.Columns.Add("ChildrenID");
            dt_Children.Columns.Add("ChildrenName");
            dt_Children.Rows.Add("1", "1", "海淀");
            dt_Children.Rows.Add("1", "2", "朝阳");
            dt_Children.Rows.Add("1", "3", "门头沟");
            dt_Children.Rows.Add("2", "4", "黄浦");
            dt_Children.Rows.Add("2", "5", "静安");
            dt_Children.Rows.Add("2", "6", "浦东新区");
            dt_Children.Rows.Add("3", "7", "郑州");
            dt_Children.Rows.Add("3", "8", "信阳");
            dt_Children.Rows.Add("4", "9", "长沙");
            dt_Children.Rows.Add("4", "10", "张家界");
            dt_Children.Rows.Add("5", "11", "广州");
            dt_Children.Rows.Add("5", "12", "深圳");

            comboBox4.DisplayMember = "ParentName";
            comboBox4.ValueMember = "ParentID";
            comboBox4.DataSource = dt_Parent;


            comboBox5.DataSource = dt_Children;
            comboBox5.DisplayMember = "ChildrenName";
            comboBox5.ValueMember = "ChildrenID";
            #endregion
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string data = comboBox1.Text;
            string value = comboBox1.SelectedValue.ToString();
            MessageBox.Show("Text:" + data + ";Value:" + value);
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            //comboBox3.DataSource = dt.Copy();
            //comboBox3.DisplayMember = "Text";
            //comboBox3.ValueMember = "Value";



            //DataTable dt2 = new DataTable();
            //dt2.Columns.Add("Value");
            //dt2.Columns.Add("Text");

            //dt2.Rows.Add("1", "C#");
            //dt2.Rows.Add("2", "PHP");
            //dt2.Rows.Add("3", "Java");
            //dt2.Rows.Add("4", "Python");
            //dt2.Rows.Add("5", "Go");
            //dt2.Rows.Add("6", "HTML");

            //comboBox2.DataSource = dt2;
            //comboBox2.DisplayMember = "Text";
            //comboBox2.ValueMember = "Value";

            //comboBox2.AutoCompleteCustomSource.Add("C#");
            //comboBox2.AutoCompleteCustomSource.Add("PHP");
            //comboBox2.AutoCompleteCustomSource.Add("Java");
            //comboBox2.AutoCompleteCustomSource.Add("Python");
            //comboBox2.AutoCompleteCustomSource.Add("Go");
            //comboBox2.AutoCompleteCustomSource.Add("HTML");
        }

        private void comboBox3_TextChanged(object sender, EventArgs e)
        {
            //DataTable dt = comboBox3.DataSource as DataTable;
            //for (int i = 0; i < dt.Rows.Count; i++)
            //{
            //    if (dt.Rows[i]["Text"].ToString().StartsWith(comboBox3.Text))
            //    {
            //        comboBox3.SelectedIndex = i;
            //        return;
            //    }
            //}
        }        

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            string value = comboBox4.SelectedValue.ToString();
            DataTable dt = dt_Children.Select("ParentID='" + value + "'").CopyToDataTable();
            comboBox5.DataSource = dt;
        }
    }
}
